/**
 * 
 * purpose: BUTTON GROUP...
 * @auth: Ning
 * date: 2009-8-20
 * 
 * */
package ui.button
{
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	
	import ui.button.ButtonEvents;
	import ui.button.NormalButtonView;

	[Event(name="button_selected",type="ui.button.ButtonEvents")]

	public class ButtonGroup extends EventDispatcher
	{
		//START...
		
		//PROPERTY...
		private var group:Array;
		
		private var _mouseEnabled:Boolean;
		
		private var _selectedButton:NormalButtonView;
		
		
		//CONSTRUCTOR...
		public function ButtonGroup()
		{
			group = new Array;
		}
		
		//METHODS...
		
		//button click handler...
		private function buttonClickHandler(evt:MouseEvent):void
		{
			var button:NormalButtonView = evt.currentTarget as NormalButtonView;

			if(button == _selectedButton)
				return;
						
			if(_selectedButton is NormalButtonView)
				_selectedButton.selected = false;
			_selectedButton = button;
			_selectedButton.selected = true;
			
			dispatchEvent(new ButtonEvents(ButtonEvents.BUTTON_SELECTED,button));
		}
		
		//add button...
		public function addButton(button:NormalButtonView):void
		{
			group.push(button);
			button.addEventListener(MouseEvent.CLICK,buttonClickHandler);
		}
		
		
		//add buttons...
		public function addButtons(...buttons):void
		{
			var buttonList:Array = buttons[0] is Array ? buttons[0] : buttons;
			var button:NormalButtonView;
			
			for(var i:uint = 0;i < buttonList.length;i++)
			{
				button = buttonList[i] as NormalButtonView;
				addButton(button);
			}
		}
		
		//select button...
		public function selectButton(button:NormalButtonView):void
		{
			var index:uint;
			for(index = 0;index < group.length;index++)
			{
				if(button == group[index])
				{
					selectButtonByIndex(index);
					return;
				}
			}
		}
		
		//select button by index...
		public function selectButtonByIndex(index:uint):void
		{
			if(index == selectedIndex)
				return;
			
			var button:NormalButtonView = group[index] as NormalButtonView;
			button.selected = true;
			button.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}		
		
		//remove button by index...
		public function removeButtonByIndex(index:uint):void
		{
			var button:NormalButtonView = group[index] as NormalButtonView;
			if(	button == _selectedButton)
			{
				button.selected = false;
				_selectedButton = null;
			}		
			
			group.splice(index,1);
			button.removeEventListener(MouseEvent.CLICK,buttonClickHandler);
		}
		
		//get button count...
		public function get buttonCount():uint
		{
			return group.length;
		}
		
		//get selected index...
		public function get selectedIndex():int
		{
			var index:int = -1;
			
			if(selectedButton is NormalButtonView)
			{
				for(var i:uint = 0;i < group.length;i++)
				{
					if(group[i] == selectedButton)
					{
						index = i;
						break;
					}
				}
			}		
			
			return index;	
		}
		
		//get selected button...
		public function get selectedButton():NormalButtonView
		{
			return _selectedButton;
		}
		
		//get button by index...
		public function getButtonByIndex(index:uint):NormalButtonView
		{
			return (group[index] as NormalButtonView);
		}
		
		//remove all
		public function removeAll():void
		{
			var button:NormalButtonView;
			for(var i:uint = 0;i < group.length;i++)
			{
				button = group[i] as NormalButtonView;
				button.removeEventListener(MouseEvent.CLICK,buttonClickHandler);	
				
				if(button == _selectedButton)
				{
					button.selected = false;
					_selectedButton = null;
				}
			}
			
			group = new Array;
			_selectedButton = null;
		}
		
		//mouse enabled...
		public function set mouseEnabled(val:Boolean):void
		{
			for(var i:uint = 0;i <group.length;i++)
			{
				if(i != selectedIndex)
					group[i].mouseEnabled = val;
			}
		}
		
		//get mouse enabled...
		public function get mouseEnabled():Boolean
		{
			return _mouseEnabled;
		}
		
		// cancel button select...
		public function cancelButtonSelect():void
		{
			if(selectedButton) 
				selectedButton.selected = false;
			
			_selectedButton = null;
		}
		
		//END
	}
}